// Created by Roman Novitsky on 03.06.2009


#region Imported Namespaces


using System.Collections.Generic;
using System.Windows;
using System.Windows.Input;

using WtfManager.Framework;
using WtfManager.UIManager;
using WtfManager.UIManager.Commands;
using WtfManager.Utilities;
using WtfManager.WindowsNative;


#endregion


namespace WtfManager.FileManager
{
  public class ShowShellContextMenuCommandHandler: Service, ICommandHandler
  {
    #region Fields


    private ConverterService _converterService;
    private UIService _uiService;


    #endregion


    #region Properties/Indexers


    public ICommand Command
    {
      get
      {
        return FileSystem.ShowShellContextMenuCommand;
      }
    }


    public override string Description
    {
      get
      {
        return "Handles Show Shell Context Menu command.";
      }
    }

    public override string Name
    {
      get
      {
        return "Show Shell Context Menu Command handler";
      }
    }


    #endregion


    #region Methods


    protected override void OnStateChnaged()
    {
      if (State == FrameworkComponentState.Initialized)
      {
        ReadOnlyServicesCollection services = WtfFramework.Instance.Services;

        _converterService = (ConverterService)services[typeof(ConverterService)];
        _uiService = (UIService)services[typeof(UIService)];
      }
      else
      {
        _converterService = null;
        _uiService = null;
      }
    }


    protected override void OnUnload()
    {
    }


    protected override FrameworkComponentState OnInitialize()
    {
      return FrameworkComponentState.Initialized;
    }


    protected override FrameworkComponentState OnLoad()
    {
      AddDependency(typeof(ConverterService));
      AddDependency(typeof(UIService));

      return FrameworkComponentState.Loaded;
    }


    public bool CanExecuteCommand(object sender, object parameter)
    {
      if (!(sender is FrameworkElement))
      {
        return false;
      }

      if(_uiService.GetCurrentWindow(sender as DependencyObject) == null)
      {
        return false;
      }

      List<FileSystemItem> fileSystemItems = GetItems((FrameworkElement)sender);

      if(fileSystemItems.Count == 0)
      {
        return false;
      }

      var parent = (FileSystemItem)fileSystemItems[0].Parent;

      if(parent == null)
      {
        return false;
      }

      for (int i = 1; i < fileSystemItems.Count; i++)
      {
        FileSystemItem item = fileSystemItems[i];

        if(item.Parent != parent)
        {
          return false;
        }
      }

      return true;
    }


    private static List<FileSystemItem> GetItems(FrameworkElement frameworkElement)
    {
      var fileSystemItems = new List<FileSystemItem>();

      if (frameworkElement == null)
      {
        return fileSystemItems;
      }

      List<object> items = frameworkElement.GetDataItems();

      foreach (object item in items)
      {
        if (item is FileSystemItem)
        {
          fileSystemItems.Add((FileSystemItem)item);
        }
      }

      if (items.Count > 0)
      {
        return fileSystemItems;
      }

      object content = frameworkElement.GetContent();

      if (content is FileSystemItem)
      {
        fileSystemItems.Add((FileSystemItem)content);
      }

      return fileSystemItems;
    }


    public void ExecuteCommand(object sender, object parameter)
    {
      List<FileSystemItem> items = GetItems((FrameworkElement)sender);

      var parent = (FileSystemItem)items[0].Parent;
      string parentPath = parent.FileSystemPath;

      var itemsNames = new List<string>();

      foreach (FileSystemItem item in items)
      {
        itemsNames.Add(item.Name);
      }

      Window currentWindow = _uiService.GetCurrentWindow(sender as DependencyObject);
      var contextMenu = new ShellContextMenu(currentWindow, parentPath, itemsNames);

      contextMenu.Show();
      contextMenu.Dispose();

      parent.Refresh();
    }


    #endregion
  }
}